package gam.n1.ask;

import gam.n1.areas.Area;
import gam.n1.areas.block.BlockSpace;
import gam.n1.areas.block.iBox;
import gam.n1.conf.ConfManager;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

import java.util.function.Supplier;

public final class AskYard implements Listener, Supplier<Area> {
    private volatile Location a = null;
    private final Area area;
    public AskYard(Area a){
        this.area = a;
    }
    @EventHandler
    public void a(PlayerInteractEvent var1){
        if (Asking.isAsking(var1.getPlayer().getUniqueId()) && var1.getAction().equals(Action.LEFT_CLICK_BLOCK)){
            Block var2 = var1.getClickedBlock();
            if (var2 != null){
                Location cB = var2.getLocation();
                if (a == null) {
                    a = cB;
                }
                ConfManager.Msg.send(var1.getPlayer(),ConfManager.Msg.getString("area.create.set.point.success"),"{point}","[" + a.getX() +"," + a.getY() + "," + a.getZ() + "]");
            }
            var1.setCancelled(true);
        }
        HandlerList.unregisterAll(this);
    }
    @Override
    public Area get() {
        while (a == null);
        if (area.getBox() != null){
            return area.getBox().s(a) ? area : null;
        } else {
            iBox b = new BlockSpace();
            b.s(a);
            area.setBox(b);
            return area;
        }
    }
}
